package com.consult.controller;


import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.consult.config.dto.AjaxResult;
import com.consult.entity.ConRecord;
import com.consult.service.IConRecordService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-06-22
 */
@RestController
@RequestMapping("/conRecord")
public class ConRecordController {

    @Resource
    public IConRecordService recordService;

    @PostMapping("/add")
    public AjaxResult addRecord(@RequestBody ConRecord record) {
        Date initDate = record.getDate();
        long appId = record.getAppId();
        String site = record.getSite();
        List<ConRecord> list = new ArrayList<>();
        list.add(record);
        for (int i = 0; i < 7; i++) {
            ConRecord conRecord = new ConRecord();
            conRecord.setSite(site);
            conRecord.setAppId(appId);
            initDate = DateUtil.offsetDay(initDate, 7);
            conRecord.setDate(initDate);
            list.add(conRecord);
        }
        recordService.saveBatch(list);
        return AjaxResult.success();
    }

    @PostMapping("/page")
    public AjaxResult getRecordPage(@RequestBody Page<ConRecord> page) {
        page = recordService.getRecordPage(page);
        return AjaxResult.success(page);
    }

    @PostMapping("/update")
    public AjaxResult updateRecord(@RequestBody ConRecord record) {
        recordService.updateById(record);
        return AjaxResult.success();
    }

}

